home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / image_extensions / histconv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.2 KB  |  257 lines

  1. /*
  2.  * Copyright 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*    histconv.c
  19.  *    A program to demonstrate the use of the histogram extension.
  20.  *    It reads an image, performs a separable convolution on it,
  21.  *    and then displays it in the left half of the window.
  22.  *    In the right half of the window the histogram is displayed for 
  23.  *    the resulting image.
  24.  * 
  25.  *    Escape key            - exit the program
  26.  */
  27. #include <GL/gl.h>
  28. #include <GL/glu.h>
  29. #include <GL/glut.h>
  30.  
  31. #include <math.h>
  32. #include <stdio.h>
  33.  
  34. #include "rgbImageFile.h"    /* should be in ../../include */
  35.  
  36. /*  Function Prototypes  */
  37.  
  38. GLvoid  initgfx( GLvoid );
  39. GLvoid  drawScene( GLvoid );
  40. GLvoid  reshape( GLsizei, GLsizei );
  41. GLvoid  keyboard( GLubyte, GLint, GLint );
  42.  
  43. void printHelp( char * );
  44.  
  45. /* Global Definitions */
  46.  
  47. #define KEY_ESC    27    /* ascii value for the escape key */
  48.  
  49. /* Global Variables */
  50.  
  51. static unsigned int     *image;
  52. static int        imgWidth, imgHeight;
  53. static GLsizei        winWidth, winHeight;
  54.  
  55. /* size of histogram table */
  56. static int        histWidth = 256;
  57.  
  58. GLvoid
  59. main( int argc, char *argv[] )
  60. {
  61.     GLsizei            width, height;
  62.  
  63.     glutInit( &argc, argv );
  64.  
  65.     if (argc < 2) {
  66.         fprintf( stderr, "usage: %s <imageFileName>\n", argv[0] );
  67.         exit (1);
  68.     }
  69.  
  70.     image = rgbReadImageFile(argv[1], &imgWidth, &imgHeight);
  71.     winWidth = imgWidth * 2;
  72.     winHeight = imgHeight;
  73.     
  74.     glutInitWindowSize( 4, 4 );
  75.     glutInitWindowSize( winWidth, winHeight ); 
  76.     glutInitDisplayMode( GLUT_RGBA );
  77.     glutCreateWindow( argv[0] );
  78.     
  79.     initgfx();
  80.  
  81.     glutKeyboardFunc( keyboard );
  82.     glutReshapeFunc( reshape );
  83.     glutDisplayFunc( drawScene ); 
  84.  
  85.     printHelp( argv[0] );
  86.  
  87.      glutMainLoop();
  88. }
  89.  
  90. GLvoid
  91. printHelp( char *progname )
  92. {
  93.     fprintf(stdout, "\n%s - use the convolution and hisgtogram "
  94.         "extensions to display a\nshifted image with its histogram\n\n"
  95.         "Escape key                - exit the program\n",
  96.         progname);
  97. }
  98.  
  99. GLvoid
  100. initgfx( void )
  101. {
  102.     if ( !glutExtensionSupported( "GL_EXT_convolution" ) ) {
  103.         fprintf(stderr, 
  104.           "EXT_convolution not supported in this implementation\n");
  105.     }
  106.     if ( !glutExtensionSupported( "GL_EXT_histogram" ) ) {
  107.         fprintf(stderr, 
  108.           "EXT_histogram not supported in this implementation\n");
  109.     }
  110.  
  111.  
  112. #ifdef    GL_EXT_histogram
  113.     glEnable(GL_HISTOGRAM_EXT);
  114.         glHistogramEXT(GL_HISTOGRAM_EXT,
  115.             histWidth /* width */,
  116.             GL_RGBA /* internalformat */,
  117.             GL_FALSE /* sink */);
  118.  
  119.     glEnable(GL_MINMAX_EXT);
  120.     glMinmaxEXT(GL_MINMAX_EXT,
  121.             GL_RGBA /* internalformat */,
  122.             GL_FALSE /* sink */);
  123. #endif
  124. }
  125.  
  126. GLvoid 
  127. keyboard( GLubyte key, GLint x, GLint y )
  128. {
  129.     switch (key) {
  130.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  131.         exit(0);
  132.     }
  133. }
  134.  
  135. GLvoid
  136. reshape( GLsizei width, GLsizei height )
  137. {
  138.     /* make sure window size is odd */
  139.     winWidth = width;
  140.     winHeight = height;
  141.  
  142.     /* Create the first viewport - the left half of the window */
  143.     glViewport( 0, 0, winWidth/2, winHeight );
  144.  
  145.     glMatrixMode( GL_PROJECTION );
  146.     glLoadIdentity();
  147.     gluOrtho2D( 0.0, (GLdouble) winWidth-1, 0.0, (GLdouble) winHeight-1 );
  148.     glMatrixMode( GL_MODELVIEW );
  149.     glLoadIdentity();
  150.     glTranslatef( 0.375, 0.375, 0.0 );
  151. }
  152.  
  153. static
  154. void displayHistogram( GLubyte *hgram, GLubyte *minmaxTable  )
  155. {
  156.     int i, j, channels = 3; /* RGB format */
  157.  
  158.     GLfloat colors[3][3];
  159.     GLfloat min[3], max[3];
  160.  
  161.     colors[0][0] = 1.0; colors[0][1] = colors[0][2] = 0.0;
  162.     colors[1][0] = 0.0; colors[1][1] = 1.0; colors[1][2] = 0.0;
  163.     colors[2][0] = 0.0; colors[2][1] = 0.0; colors[2][2] = 1.0;
  164.     min[0] = minmaxTable[0]; max[0] = minmaxTable[3];
  165.     min[1] = minmaxTable[1]; max[1] = minmaxTable[4];
  166.     min[2] = minmaxTable[2]; max[2] = minmaxTable[5];
  167.  
  168.     for (i = 0; i < channels; i++, hgram++) {
  169.         GLubyte *next, *cur;
  170.         GLfloat y, scale, height = imgHeight/channels;
  171.  
  172.         glColor3f(colors[i][0],colors[i][1],colors[i][2]);
  173.  
  174.         cur = hgram;
  175.         next = cur + channels;
  176.         for ( j = 0; j < histWidth; j++ ) {
  177.             if ( *cur < *next ) { cur = next; }
  178.             next += channels;
  179.         }
  180.         scale = (*cur == 0 ? scale = 0.0 : height / (GLfloat)(*cur));
  181.  
  182.         fprintf(stderr, "min[%d]=%f, max[%d]=%f, scale=%f\n", 
  183.             i, min[i], i, max[i], scale);
  184.  
  185.         glBegin(GL_LINE_STRIP);
  186.             next = (GLubyte *)hgram;
  187.             for (j = 0; j < histWidth; j++, next += channels) {
  188.                 y = *next * scale;
  189.                 glVertex2i(j, (GLuint) y);
  190.             }
  191.         glEnd();
  192.                 glTranslatef (0, height, 0.0);
  193.         }
  194. }
  195.  
  196. static GLubyte hgramTable[4096*4];
  197. static GLubyte minmaxTable[3*2*1];
  198.     
  199. GLvoid
  200. drawScene( GLvoid )
  201. {
  202.     static GLfloat separableImage[10] = { 0.5, 0.0, 0.0, 0.0, 0.5, 
  203.                           0.5, 0.0, 0.0, 0.0, 0.5
  204.                         };
  205.  
  206.     /* Create the first viewport - the left half of the window */
  207.     glViewport( 0, 0, winWidth/2, winHeight );
  208.  
  209.     glClear( GL_COLOR_BUFFER_BIT );
  210.     
  211. #ifdef    GL_EXT_convolution
  212.     glEnable(GL_SEPARABLE_2D_EXT);
  213.  
  214.         glSeparableFilter2DEXT(GL_SEPARABLE_2D_EXT,
  215.                 GL_LUMINANCE,
  216.                 5, 5,
  217.                 GL_LUMINANCE,
  218.                 GL_FLOAT,
  219.                 (const void*)separableImage,
  220.                 (const void*)&separableImage[5]);
  221. #endif
  222.  
  223.     glRasterPos2f( 0, 0 );
  224.     glDrawPixels( imgWidth, imgHeight, GL_RGBA, GL_UNSIGNED_BYTE, image ); 
  225.  
  226. #ifdef    GL_EXT_convolution
  227.     glDisable(GL_SEPARABLE_2D_EXT);
  228. #endif
  229.  
  230. #ifdef    GL_EXT_histogram
  231.     glGetHistogramParameterivEXT(GL_HISTOGRAM_EXT,
  232.                 GL_HISTOGRAM_WIDTH_EXT,
  233.                 &histWidth);
  234.  
  235.     glGetHistogramEXT(GL_HISTOGRAM_EXT,
  236.                 GL_FALSE, /* reset */
  237.                 GL_RGB, /* return format */
  238.                 GL_UNSIGNED_BYTE, /* return type */
  239.                 (GLvoid *)hgramTable);
  240.  
  241.     glGetMinmaxEXT(GL_MINMAX_EXT,
  242.                 GL_FALSE, /* reset */
  243.                 GL_RGB, /* return format */
  244.                 GL_UNSIGNED_BYTE, /* return type */
  245.                 (GLvoid *)minmaxTable);
  246. #endif
  247.  
  248.     /* Create the second viewport - right half of window */
  249.     glViewport( winWidth/2 + 1, 0, winWidth/2, winHeight );
  250.  
  251.     displayHistogram( hgramTable, minmaxTable ); 
  252.  
  253.     glFlush();
  254.  
  255.     checkError("drawScene");
  256. }
  257.